package org.dtrd.modules.faq.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.faq.entity.dto.FaqDetail;
import org.dtrd.modules.faq.entity.dto.FaqListInfo;
import org.dtrd.modules.faq.entity.request.FaqListRequest;
import org.dtrd.modules.faq.service.IDtrdExtEmsYwFaqsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 知识问答 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-06
 */
@Api(tags = "常见问题")
@RestController
@RequestMapping("/100/15/")
public class FaqsController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwFaqsService faqsService;


    @ApiOperation(value = "获取常见问题详情", response = FaqDetail.class)
    @GetMapping("1001505")
    @AutoLog(value="获取常见问题详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFaq(@RequestParam Integer id){
        FaqDetail faq = faqsService.getFaq(id);
        return Result.OK(faq);
    }

    @ApiOperation(value = "分页获取常见问题列表", response = FaqListInfo.class)
    @PostMapping("1001506")
    @AutoLog(value="分页获取常见问题列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFaqList(@RequestBody FaqListRequest request){
        // 处理分页数据
        Integer pageNum = request.getPage();
        Integer pageSize = request.getPageSize();
        pageNum = getIntParam(pageNum, 1);
        pageSize = getPageSize(pageSize, 20);
        request.setPage(pageNum);
        request.setPageSize(pageSize);
        IPage<FaqListInfo> page = faqsService.pageFaq(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改常见问题内容")
    @PostMapping("1001507")
    @AutoLog(value="新增或修改常见问题内容", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateFaq(@RequestBody FaqDetail faqDetail){
        // 分类id 和 问题不能为空
        if (isIntParamInvalid(faqDetail.getFaqCategoryid())
                || StrUtil.isBlank(faqDetail.getFaqQuestion())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = faqsService.saveOrUpdateFaq(faqDetail);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除常见内容")
    @DeleteMapping("1001508")
    @AutoLog(value="删除常见内容", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeFaq(@RequestParam Integer id){
        if (isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = faqsService.removeFaq(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
